package com.chat.api.web.restctrl;

import com.chat.api.utils.ResultUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.service.ChatService;
import com.chat.api.web.vo.MessageEntity;
import com.chat.api.websocket.MyWebSocket;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/chat")
public class ChatCtrl {

    @Autowired
    private ChatService chatService;

    @RequestMapping("test")
    @ApiOperation("点赞or取消赞")
    public String clickLike(HttpServletRequest request) throws IOException {
//        String s = WebSocket.sendMessage();
        return "";
    }

    @RequestMapping(value = "findHistoryInfo",method = RequestMethod.POST)
    @ApiOperation("获取聊天记录")
    public MessageEntity findHistoryInfo(@RequestParam(value = "id") String idStr, HttpServletRequest request) throws IOException {
        Long receiver = ParamUtil.getLongParameter(idStr, -1L);
        if (receiver < 0){
            return ResultUtil.fail("用户id不能为空");
        }
        return chatService.getChatLog(receiver,request);
    }

    @RequestMapping("saveMsg")
    @ApiOperation("将消息同步到数据库")
    public MessageEntity saveMsg(@RequestParam(value = "receiveId") String receiveId,
                          @RequestParam(value = "message") String message,HttpServletRequest request) throws IOException {
        if (StringUtils.isBlank(receiveId)){
            return ResultUtil.fail("receiveId不能为空");
        }
        Long rid = ParamUtil.getLongParameter(receiveId, -1L);
        return chatService.saveMsg(rid,message,request);
    }

    @RequestMapping("noReadCount")
    @ApiOperation("获取未读消息数")
    public MessageEntity noReadCount(HttpServletRequest request) throws IOException {
        return chatService.getNoReadCount(request);
    }

    @RequestMapping(value = "/index")
    public void index() {
        MyWebSocket.pushMessage("群主", "你们好", null);
    }

    @RequestMapping("getOnlineNum")
    @ApiOperation("将消息同步到数据库")
    public MessageEntity getOnlineNum(HttpServletRequest request) throws IOException {
        return chatService.getOnlineNum();
    }


    @RequestMapping(value = "contactLT",method = RequestMethod.POST)
    @ApiOperation("娱乐版块联系站主")
    public MessageEntity contactLT(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String fullname = map.get("fullname");
        String email = map.get("email");
        String iphoneNumber = map.get("iphoneNumber");
        String message = map.get("message");
        return chatService.sendLT(request,fullname,email,iphoneNumber,message);
    }

    @RequestMapping("vedio")
    @ApiOperation("将消息同步到数据库")
    public MessageEntity vedio(@RequestParam(value = "receiver") String receiver,
                               @RequestParam(value = "data") String data,
                               HttpServletRequest request) throws IOException {
        WebSocketServer.sendInfo(data,receiver);
        return chatService.getOnlineNum();
    }

}
